package com.mdc.mansion.web.modules.restaurant.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.mdc.mansion.web.modules.system.utils.UserUtils;
import com.mdc.mansion.dataaccess.modules.system.entity.User;

import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantConfig;
import com.mdc.mansion.core.modules.restaurant.RestaurantConfigService;
import com.mdc.mansion.web.common.web.BaseController;
/**
 * 餐厅配置信息Controller
 * @author mingdongc
 * @version 2018-01-01
 */
@Controller
@RequestMapping(value = "${adminPath}/restaurant/restaurantConfig")
public class RestaurantConfigController extends BaseController {

	@Autowired
	private RestaurantConfigService restaurantConfigService;
	
	@ModelAttribute
	public RestaurantConfig get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return restaurantConfigService.get(id);
		}else{
			return new RestaurantConfig();
		}
	}
	
	@RequiresPermissions("restaurant:restaurantConfig:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(RestaurantConfig restaurantConfig) {
		Map<String, Object> model=new HashMap<String, Object>();
		Page<RestaurantConfig> page = restaurantConfigService.findPage(restaurantConfig);
        model.put("page", page);
		ModelAndView modelAndView=new ModelAndView("modules/restaurant/restaurantConfigList", model);
		return modelAndView;
	}
	
	@RequiresPermissions("restaurant:restaurantConfig:view")
	@RequestMapping(value = "form")
	public ModelAndView form(RestaurantConfig restaurantConfig) {
		Map<String, Object> model=new HashMap<String, Object>();
		model.put("restaurantConfig", restaurantConfig);
		return new ModelAndView("modules/restaurant/restaurantConfigForm",model);
	}
	

	@RequiresPermissions("restaurant:restaurantConfig:edit")
	@RequestMapping(value = "save")
	public ModelAndView save(RestaurantConfig restaurantConfig,RedirectAttributes redirectAttributes) {
		User u=UserUtils.getUser();
		restaurantConfig.setUpdateBy(u);
		if (StringUtils.isNotBlank(restaurantConfig.getId())){
			restaurantConfigService.updateSelective(restaurantConfig);
		}else{
			restaurantConfig.setCreateBy(u);
			restaurantConfigService.save(restaurantConfig);
		}
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	@RequiresPermissions("restaurant:restaurantConfig:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(RestaurantConfig restaurantConfig,RedirectAttributes redirectAttributes) {
		restaurantConfigService.delete(restaurantConfig);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
}
